package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.MarketTopicService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MarketTopicServiceImpl implements MarketTopicService {


    @Override
    public List<MarketTopic> getTopics(String page, String limit, String title, String subtitle, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        //创建查询条件
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.or().andTitleLike("%" + title + "%").
                andSubtitleLike("%" + subtitle + "%").andDeletedEqualTo(false);
        marketTopicExample.orderBy(sort + " " + order);
        //分页插件分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        //执行查询
        List<MarketTopic> marketTopics = mapper.selectByExampleSelective(marketTopicExample);
        session.commit();
        session.close();
        return marketTopics;
    }

    @Override
    public Map<String, Object> getTopicById(String id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        MarketGoodsMapper goodsMapper = session.getMapper(MarketGoodsMapper.class);
        //根据页面所传id获取专题实体类
        MarketTopic marketTopic = mapper.selectByPrimaryKey(Integer.parseInt(id));
        Map<String, Object> Topic = new HashMap<>();
        //获取商品对应Id的整型数组
        Integer[] goods = marketTopic.getGoods();
        ArrayList<MarketGoods> goodsArrayList = new ArrayList<>();
        //遍历商品的id，获取数组
        if (goods != null) {
            for (Integer good : goods) {
                MarketGoods marketGoods = goodsMapper.selectByPrimaryKeySelective(good, MarketGoods.Column.id, MarketGoods.Column.name,
                        MarketGoods.Column.brief, MarketGoods.Column.picUrl, MarketGoods.Column.isNew, MarketGoods.Column.isHot,
                        MarketGoods.Column.counterPrice, MarketGoods.Column.retailPrice);
                goodsArrayList.add(marketGoods);
            }
        }
        //封装数据
        Topic.put("topic", marketTopic);
        Topic.put("goodsList", goodsArrayList);
        session.commit();
        session.close();
        return Topic;
    }

    @Override
    public MarketTopic updateTopic(MarketTopic record) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        //修改专题信息
        mapper.updateByPrimaryKey(record);
        session.commit();
        session.close();
        return record;
    }

    @Override
    public void deleteTopic(String id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        //用专题实体类设置逻辑删除的条件
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setId(Integer.parseInt(id));
        marketTopic.setDeleted(true);
        //删除对应专题
        mapper.updateByPrimaryKeySelective(marketTopic);
        session.commit();
        session.close();
    }

    @Override
    public MarketTopic createTopic(MarketTopic marketTopic) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        mapper.insertSelective(marketTopic);
        MarketTopic resultTopic = mapper.selectByPrimaryKeySelective(marketTopic.getId(), MarketTopic.Column.id,
                MarketTopic.Column.title, MarketTopic.Column.subtitle, MarketTopic.Column.price,
                MarketTopic.Column.picUrl, MarketTopic.Column.goods, MarketTopic.Column.addTime,
                MarketTopic.Column.updateTime, MarketTopic.Column.content);
        session.commit();
        session.close();
        return resultTopic;
    }
}
